require(["sys", "validate"], function(s) {
	$(function() {
		let form = $("form:eq(0)");
		form.on("submit", function() {
			$.post(
				"user/login.do",
				form.serialize(),
				function(data) {
					if(data.ok) {
						s.s("登录成功");
						window.location = "./";
					} else {
						s.e(data.message);
						$("#code").trigger("click");
					}
				}, "json"
			);

			return false;
		});

		let regForm = $("form:eq(1)");
		regForm.bootstrapValidator({
			//container: 'tooltip',
			feedbackIcons: {
				valid: 'glyphicon glyphicon-ok',
				invalid: 'glyphicon glyphicon-remove',
				validating: 'glyphicon glyphicon-refresh'
			},
			fields: {
				userName: {
					validators: {
						notEmpty: {
							message: "用户名不能为空"
						},
						regexp: {
							enabled: true,
							regexp: /^[a-z]\w{2,19}$/i,
							message: '用户名必须为首字符为字母的3-20个单词字符组成'
						},
						remote: {
							type: 'POST',
							url: 'user/exists-name.do',
							message: '用户名重复',
							delay: 1000
						},
						different: {
							field: 'userPwd',
							message: '用户名和密码不能一样'
						}

					}
				},
				userRealName: {
					validators: {
						notEmpty: {
							message: "昵称不能为空"
						}
					}
				},
				userPwd: {
					validators: {
						notEmpty: {
							message: "密码不能为空"
						},
						regexp: {
							enabled: true,
							regexp: /^.{3,20}$/i,
							message: '密码必须为3-20个字符组成'
						},
						identical: {
							field: 'userPwd1',
							message: '两次输入的密码不一致'
						},
						different: {
							field: 'userName',
							message: '密码不能和用户名一样'
						}
					}
				},
				userPwd1: {
					validators: {

						identical: {
							field: 'userPwd',
							message: '两次输入的密码不一致'
						}
					}
				},
				userTel: {
					validators: {
						regexp: {
							enabled: true,
							regexp: /^1\d{10}$/,
							message: '手机号码格式不正确'
						},
						remote: {
							type: 'POST',
							url: 'user/exists-tel.do',
							message: '手机号码不能重复',
							delay: 1000
						},
					}
				},
				code: {
					validators: {
						notEmpty: {
							message: "手机验证码不能为空"
						},
						regexp: {
							enabled: true,
							regexp: /^\d{6}$/,
							message: '手机验证码为6位数组组成'
						}

					}
				}
			}
		}).on("success.form.bv",function(){
			$.post(
				"user/reg.do",
				regForm.serializeArray(),
				function(data){
					if(data.ok){
						s.s("注册成功");
						regForm.trigger("reset");
						$("._show1").trigger("click");
						regForm.bootstrapValidator('disableSubmitButtons', false).bootstrapValidator('resetForm', true);
					}else{
						s.e(data.message);
						regForm.bootstrapValidator('disableSubmitButtons', false);
					}
				},"json"
			);
			return false;
		});


		let codeBtn = $("._codeBtn");
		codeBtn.click(function(){
			if(!codeBtn[0].disabled){
				let tel = regForm.find("[name='userTel']").val();
				if(!/^1\d{10}$/.test(tel)){
					s.e("手机号码格式错误");
					return;
				}
				$.post(
					"user/tel-code.do",
					{"userTel":tel},
					function(data){
						if(data.ok){
							s.s("发送手机验证码成功");
						}else{
							s.e(data.message);
						}
					},"json"
				);
				let btnText = codeBtn.text();
				codeBtn[0].disabled=true;
				let max=120;
				codeBtn.text(max+"s");
				let timer = window.setInterval(function(){
					codeBtn.text(--max+"s");
					if(max<=0){
						codeBtn[0].disabled=false;
						codeBtn.text(btnText);
						window.clearInterval(timer);
					}
				},1000);
			}
		});

		$("._show1").click(function() {
			$("body > div.panel:eq(0)").show();
			$("body > div.panel:eq(1)").hide();
		});

		$("._show2").click(function() {
			$("body > div.panel:eq(1)").show();
			$("body > div.panel:eq(0)").hide();
		});
	});
});